$(function () {

    //表单验证器的初始化
    $('#userAddForm').bootstrapValidator({
        excluded:[":disabled"],
        feedbackIcons: {  //显示表单验证结果的图标
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            name: {
                validators: {
                    notEmpty: {
                        message: '角色名称不能为空！'
                    },
                    stringLength: {
                        min: 2,
                        max: 10,
                        message: '角色名长度不能小于2位或超过10位'
                    }
                }
            },

            bonus: {
                validators: {
                    notEmpty: {
                        message: '角色奖金不能为空！'
                    },
                    stringLength: {
                        min: 2,
                        max: 10,
                        message: '角色奖金长度不能小于2位或超过10位'
                    }
                }
            },

            beizhu: {
                validators: {
                    notEmpty: {
                        message: '角色备注不能为空！'
                    },
                    stringLength: {
                        min: 2,
                        max: 10,
                        message: '角色备注长度不能小于2位或超过10位'
                    }
                }
            }
        }
    });


    
});

var columns = [
    {
        title: '角色名称',
        dataIndex:'name',
        align:'center',
        sorter:(a, b) => a.name.localeCompare(b.name),
        width: '15%'
    }, {
       title: '奖金',
        dataIndex:'bonus',
        width: '15%'
}, {
    title: '备注',
        dataIndex:'beizhu',
        width: '15%'
}, {
    title: '上次活动时间',
        dataIndex:'time',
        width: '15%'
}, {
        title: '操作',
        dataIndex:'',
        align:'center',
        scopedSlots: { customRender: 'action'},
        width: '25%'
}
];
//分页设置
var pagination = {
    showSizeChanger: true,//每页显示多少条记录是否需要?
    showQuickJumper:true,//跳到第几页要不要？
    pageSizeOptions:['5', '10', '15'],
    pageSize:5,//每页的数据条数
    current: 1,//当前页
    position:'bottom',//top,both,bottom，显示的位置
    total: 0,//数据的总条数
    onChange: (current) => { // 切换分页时的回调，
        // 当在页面定义change事件时，切记要把此处的事件清除，因为这两个事件重叠了，可能到时候会导致一些莫名的bug
        this.pagination.current = current;
    },
};
var v = new Vue({
    el:'#app',
    data () {
        return {
            info: null,
            id:null,
            name:'',
            pass:'',
            beizhu:'',
            bonus:'',
            modalTitle:'',
            key:'save',
            columns:columns,
            dataSources:null,
            pagination:pagination,
            selectedRowKeys:[],
            selectedRows:[],
            menuids:[],
            treeData:[],

        }
    },
    methods:{
        //加载树形选择
        loadTreeData(){
            axios({
                url:'org/getAllTreeData.do',
                method:'post'

            }).then(function (res) {
                v.treeData = res.data;
            }).catch(function (reason) {
            });
        },
        orgTreeChange(){

        },

        //自定义行
        customRow(record, index){
            return{
                style:{
                    'background-color':'#ffffff'
                },
                on:{
                    click: (event) => {
                    if(this.selectedRowKeys.length>0 && this.selectedRowKeys.includes(record.id)){
                       this.selectedRowKeys.splice(this.selectedRowKeys.indexOf(record.id),1);
                    }else
                     {
                      this.selectedRowKeys.push(record.id);
                      }
                 }
                 }
                }

        },
        //行选中事件
        onSelectChange(selectedRowKeys,selectedRows){
            this.selectedRowKeys = selectedRowKeys;
            this.selectedRows = selectedRows;
        },
        //提交操作
        operate(){
            if(this.key=='shouquan'){
                //保存授权
                this.saveShouQuan();

            }else{
                //触发表单验证
                $('#userAddForm').data('bootstrapValidator').validate();//验证
                var tag = $('#userAddForm').data('bootstrapValidator').isValid();//验证状态true或false
                if(tag){

                    if(this.key=='save'){
                        this.saveData();
                    } else{
                        this.updateData();
                    }
                }
            }

        },
        //打开添加对话框
        openAdd(){
            //重置验证
            $('#userAddForm').data("bootstrapValidator").resetForm();

            this.clearData();
            $('#myModal').modal('show');
            this.modalTitle = '用户添加';
            this.key = 'save';
            this.loadTreeData();

        },
        //加载树形选择
        loadTreeData(){
            axios({
                url:'menu/getAllTreeData.do',
                method:'post'

            }).then(function (res) {
                v.treeData = res.data;
            }).catch(function (reason) {
            });
        },
        //保存授权
        saveShouQuan(){
            axios({
                url:'role/saveShouQuan.do',
                method:'post',
                data:{
                    id:this.id,
                    menuids:this.menuids
                }

            }).then(function (res) {
                if(res.data.result=='success'){
                    alert('保存成功！');
                    //关闭模态框
                    $('#shouQuanModal').modal('hide');
                }else{
                    alert('保存失败！');
                }

            }).catch(function (reason) {

            });
        },
        //打开授权界面
        openShouQuan(item){
            this.menuids=[];
            this.id = item.id;
            this.key = 'shouquan';
            //显示已经授权的菜单
            axios({
                url:'role/getShouQuan.do',
                method:'post',
                data:{
                    id:this.id
                }

            }).then(function (res) {

                //将查询的菜单ids赋给menuids
                if(res.data!="" && res.data!=null) {
                    var ids = res.data + "";//要进行转换，否则response.data是数字，无法使用split方法处理
                    v.menuids = ids.split(",");
                }

            }).catch(function (reason) {

            });
            $('#shouQuanModal').modal('show');
            this.loadTreeData();

        },
        //打开修改对话框
        openUpdate(item){
            this.clearData();
            //重置验证
            $('#userAddForm').data("bootstrapValidator").resetForm();
            $('#myModal').modal('show');
            this.modalTitle = '用户修改';
            this.name = item.name;
            this.bonus = item.bonus;
            this.beizhu = item.beizhu;
            this.id = item.id;
            this.key = 'update';
            this.loadTreeData();

        },
        //加载数据
       loadData(){
           axios({
               url:'role/getAllData.do',
               method:'post'

           }).then(function (res) {
               v.dataSources = res.data;
           }).catch(function (reason) {
               alert('请求失败')
           });
       },
        clearData(){
            this.name = '';
            this.beizhu = '';
            this.bonus = '';
        },
        //查询用户
        selectData(){
            axios({
                url:'role/selectData.do',
                method:'post',
                data:{name:this.name}
            }).then(function (res) {
                v.dataSources = res.data;
            }).catch(function (reason) {
                alert('请求失败')
            });

        },
       //删除用户
        deleteData(id){
            axios({
                url:'role/deleteData.do',
                method:'post',
                data:{
                    id:id
                }

            }).then(function (res) {
                if(res.data.result=='success'){
                    alert('删除成功！');
                    //刷新数据
                    v.loadData();
                }else{
                    alert('删除失败！');
                }

            }).catch(function (reason) {

            });
        },
        //修改
        updateData(){
            axios({
                url:'role/updateData.do',
                method:'post',
                data:{
                    name:this.name,
                    beizhu:this.beizhu,
                    bonus:this.bonus,
                    id:this.id
                }

            }).then(function (res) {
                if(res.data.result=='success'){
                    alert('修改成功！');
                    //关闭模态框
                    $('#myModal').modal('hide');
                    v.clearData();
                    //刷新数据
                    v.loadData();
                }else{
                    alert('修改失败！');
                }

            }).catch(function (reason) {

            });
        },
        //保存
        saveData(){
            axios({
                url:'role/saveData.do',
                method:'post',
                data:{
                    name:this.name,
                    beizhu:this.beizhu,
                    bonus:this.bonus
                }

            }).then(function (res) {
               if(res.data.result=='success'){
                   alert('保存成功！');
                   //关闭模态框
                   $('#myModal').modal('hide');
                   v.clearData();
                   //刷新数据
                   v.loadData();
               }else{
                   alert('保存失败！');
               }

            }).catch(function (reason) {

            });
        }

    },
    mounted () {
        this.loadData();
    }

});